package com.zjy.software.day19;

import java.sql.*;

public class JDBCDemo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
//        testInsert();

//        testSelect();

//        testUnSafeLogin();

        String userName = "zs";
        String password = "123456";

        //1、注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2、创建连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zjy", "root", "password");
        System.out.println(connection);
        //3、得到执行sql语句的Statement对象
        String loginSql = "select * from users where name=? and password=?";
        PreparedStatement preparedStatement = connection.prepareStatement(loginSql);
        preparedStatement.setObject(1, userName);
        preparedStatement.setObject(2, password);
        //4、执行sql语句，并返回结果

        ResultSet resultSet = preparedStatement.executeQuery(loginSql);

        if (resultSet.next()) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }
        //5、处理结果
//        resultSet.next();
        //6关闭资源
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }

    private static void testUnSafeLogin() throws ClassNotFoundException, SQLException {
        String userName = "zs324324325454545423432423";
        String password = "123456324324324234324324234' or '1'='1";

        //1、注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2、创建连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zjy", "root", "password");
        System.out.println(connection);
        //3、得到执行sql语句的Statement对象
        Statement statement = connection.createStatement();
        //4、执行sql语句，并返回结果
        String loginSql = "select   * from users where name = '" + userName + "' and password = '" + password + "'";
        ResultSet resultSet = statement.executeQuery(loginSql);

        if (resultSet.next()) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }
        //5、处理结果
//        resultSet.next();
        //6关闭资源
        resultSet.close();
        statement.close();
        connection.close();
    }

    private static void testSelect() throws ClassNotFoundException, SQLException {
        //1、注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2、创建连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zjy", "root", "password");
        System.out.println(connection);
        //3、得到执行sql语句的Statement对象
        Statement statement = connection.createStatement();
        //4、执行sql语句，并返回结果
        ResultSet resultSet = statement.executeQuery("select   * from users");
        //5、处理结果
//        resultSet.next();
        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String password = resultSet.getString(3);
            String email = resultSet.getString(4);
            System.out.println("id = " + id + ", name = " + name + ", password = " + password + ", email = " + email);
        }
//        int id = resultSet.getInt(1);
//        System.out.println("id = " + id);
//        System.out.println("resultSet.getString(\"name\") = " + resultSet.getString("name"));
//        System.out.println("resultSet.getObject(\"password\") = " + resultSet.getObject("password"));
        //6关闭资源
        resultSet.close();
        statement.close();
        connection.close();
    }

    private static void testInsert() throws ClassNotFoundException, SQLException {
        //1、注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2、创建连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zjy", "root", "password");
        System.out.println(connection);
        //3、得到执行sql语句的Statement对象
        Statement statement = connection.createStatement();
        //4、执行sql语句，并返回结果
        int i = statement.executeUpdate("insert into users(id,name,password,email) values (5,'旺财','root','samin@qq.com')");
        //5、处理结果
        System.out.println("i = " + i);
        //6关闭资源
        statement.close();
        connection.close();
    }
}

